# 三门问题代码模拟
# 引用自： https://blog.csdn.net/u011054333/article/details/84449503


def three_door_question(times: int):
    import random
    result_if_not_change = 0
    result_if_change = 0
    for i in range(0, times):
        # 洗牌阶段
        doors = [1, 2, 3]  # 3是大奖
        random.shuffle(doors)

        # 第一次抽奖阶段
        first_choice = doors[random.randint(0, 2)]
        doors.remove(first_choice)

        # 第二阶段-开门
        # 如果大奖在剩下的里面，由主持人排除一个错误答案，剩下大奖
        if 3 in doors:
            doors = [3]
        # 如果大奖已经被选了，主持人随机排除剩下一个错误答案
        else:
            doors.remove(random.choice((1, 2)))

        # 第三阶段——看概率
        if first_choice == 3:
            result_if_not_change = result_if_not_change + 1
        if doors[0] == 3:
            result_if_change = result_if_change + 1

    print(
        f'Total times:{times}, prob of not change is {result_if_not_change / times}, prob of change is {result_if_change / times}')


three_door_question(10000)
three_door_question(100000)
three_door_question(1000000)

# 运行结果
'''
Total times:10000, prob of not change is 0.329, prob of change is 0.671
Total times:100000, prob of not change is 0.33306, prob of change is 0.66694
Total times:1000000, prob of not change is 0.333483, prob of change is 0.666517
'''
